package view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by My on 2016/4/6.
 */
public class Custom extends View {
    Context context;
    Paint linePaint, pathPaint, dashPaintSelect,dashPaintDefault;
    int foldingHeight;//折线的高度
    Path pointPath,dashPath;//点的路径
    int[] test = new int[]{50, 10, 80, 100,120,134,232,134,134};//测试数据
    int radius = 10;//圆的半径
    int radiusDis = 10;//大圆的半径于小圆半径的差
    int bigRadiusColor=Color.parseColor("#50FF4C2E");//大圆的颜色
    int smallRadiusColor=Color.parseColor("#FF4C2E");//小圆的颜色
    int baseHeight = 300;//折线的基础高度
    int sideLineColor=Color.parseColor("#FF4C2E");//边线的颜色
    int pathCenterColor=Color.parseColor("#FAA394");//图表内部的颜色
//    int dashLineColor=BLUECOLOR;//虚线的颜色
    int defaultDashLineWidth =2;//默认虚线的宽度
    int dashLineWidthSelect=7;//选中虚线的宽度
    int  chartHeight;//图表的高度
    int  chartPaddingBottom=100;//图表的高度
    int  textPaddingBottom=20;//文字距离底部的距离
    int  textSize=25;//文字的大小
    String  textUnit="个月";
    int  linePaintWidth=2;//边线的宽度
    int zoomFacter =2;//落差放大系数
    int  textViewDisCenter;//文字中心点的偏移量
    int  BLUECOLOR=Color.parseColor("#4A90E2");
    int  LIGHTGRAY=Color.parseColor("#999999");
    ArrayList<Pot> totalPots = new ArrayList<Pot>();
    ArrayList<Pot> pots = new ArrayList<Pot>();
    ArrayList<TextView> viewList = new ArrayList<TextView>();

    int  clickPosition;
    public Custom(Context context) {
        this(context, null);
    }

    public Custom(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Custom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        linePaint = new Paint();
        pathPaint = new Paint();
        dashPaintSelect = new Paint(Paint.ANTI_ALIAS_FLAG);
        foldingHeight = chartHeight / 4;
        pointPath = new Path();
        dashPath = new Path();
        dashPaintSelect.setStyle(Paint.Style.STROKE);
        dashPaintSelect.setColor(BLUECOLOR);
        PathEffect effects = new DashPathEffect(new float[] {3,3,5},0);
        dashPaintSelect.setPathEffect(effects);


        dashPaintDefault=new Paint(Paint.ANTI_ALIAS_FLAG);
        dashPaintDefault.setStyle(Paint.Style.STROKE);
        dashPaintDefault.setColor(BLUECOLOR);
        PathEffect effects2 = new DashPathEffect(new float[] {10,3,5},0);
        dashPaintDefault.setPathEffect(effects2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(widthMeasureSpec);
        int height = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            width = getPaddingLeft() + getPaddingRight();
        }
        if (heightMode != MeasureSpec.EXACTLY) {
            height = getPaddingTop() + getPaddingBottom();
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        pathPaint.setColor(pathCenterColor);
        linePaint.setStrokeWidth(linePaintWidth);
        linePaint.setColor(sideLineColor);
        linePaint.setStrokeWidth(linePaintWidth);
        canvas.drawPath(pointPath, pathPaint);
        canvas.drawLine(0, chartHeight, getWidth(), chartHeight, linePaint);
        for (int i = 0; i < totalPots.size() - 1; i++) {
            Pot pt = totalPots.get(i);
            Pot pt2 = totalPots.get(i + 1);
            canvas.drawLine(pt.x, pt.y, pt2.x, pt2.y, linePaint);
        }
        for (int i = 0; i < pots.size(); i++) {
            Pot pt = pots.get(i);
            pathPaint.setTextSize(textSize);
            if (clickPosition==i){
                pathPaint.setColor(bigRadiusColor);
                canvas.drawCircle(pt.x, pt.y, radius + radiusDis, pathPaint);
                pathPaint.setColor(smallRadiusColor);
                canvas.drawCircle(pt.x, pt.y, radius, pathPaint);
                //绘制字体
                pathPaint.setColor(BLUECOLOR);
                canvas.drawText((i + 1) + textUnit, pt.x - textViewDisCenter, getHeight() - textPaddingBottom, pathPaint);
//                //绘制虚线
//                dashPaintSelect.setStrokeWidth(dashLineWidthSelect);
//                dashPath.moveTo(pt.x, pt.y + radius + radiusDis);
//                dashPath.lineTo(pt.x, chartHeight);
//                canvas.drawPath(dashPath, dashPaintSelect);
                TextPaint  textPaint=new TextPaint();
                textPaint.setTextSize(25);
                textPaint.setColor(smallRadiusColor);
                float  textLength= textPaint.measureText("10%");
                RectF  rf=new RectF(pt.x-(int)(textLength/2)-10,pt.y-radius-radiusDis-50,pt.x+(int)(textLength/2)+10,pt.y-radius-radiusDis-10);
                canvas.drawRoundRect(rf, 12, 15, textPaint);
                textPaint.setColor(Color.WHITE);
                canvas.drawText("10%",pt.x-(int)(textLength/2),pt.y-radius-radiusDis-20,textPaint);
            }else{
                pathPaint.setColor(smallRadiusColor);
                canvas.drawCircle(pt.x, pt.y, radius, pathPaint);
                //绘制字体
                pathPaint.setColor(LIGHTGRAY);
                canvas.drawText((i + 1) + textUnit, pt.x - textViewDisCenter, getHeight() - textPaddingBottom, pathPaint);

            }
            //绘制虚线
            dashPaintDefault.setStrokeWidth(defaultDashLineWidth);
            dashPath.moveTo(pt.x, pt.y + radius + radiusDis);
            dashPath.lineTo(pt.x, chartHeight);
            canvas.drawPath(dashPath, dashPaintDefault);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
           float  X= event.getX();
        Log.e("TAG","X>"+X);
        if(event.getAction()==MotionEvent.ACTION_MOVE){


        }
        if(event.getAction()==MotionEvent.ACTION_UP){
            for (int i=0;i<test.length;i++){
                Pot    pt=pots.get(i);
                if(pt.x-50<X  && X<pt.x+50){
                    Log.e("TAG","X>"+X);
                    clickPosition=i;
                    invalidate();
                    return true;
                }
            }
        }
        return true;
    }
    public void set() {
        dashPath.reset();
        pointPath.reset();
        totalPots.clear();
        pots.clear();
        totalPots.clear();


        chartHeight=getHeight()-chartPaddingBottom;
        //把View分成N+1份
        int space = getWidth() / (test.length + 1);
        Log.e("TAG", "getWidth():" + getWidth() + "  test.length:" + test.length);
        Log.e("TAG", "space:" + space);
        //把开始点移出屏幕3/1
        pointPath.moveTo(-getWidth() / 3, chartHeight);
        //记录屏幕外第一个点的坐标
        Pot pot = new Pot();
        pot.x = -getWidth() / 3;
        pot.y = chartHeight;
        totalPots.add(pot);
        //记录可见view最左边的坐标
        Pot po = new Pot();
        po.x = 0;
        po.y = chartHeight - (test[0] + baseHeight);
        totalPots.add(po);
        pointPath.lineTo(0, chartHeight - (test[0] + baseHeight));
        for (int i = 1; i <= test.length; i++) {
            pointPath.lineTo(i * space, chartHeight - (test[i - 1]* zoomFacter + baseHeight));
            Pot pot1 = new Pot();
            pot1.x = i * space;
            pot1.y = chartHeight - (test[i - 1]* zoomFacter + baseHeight);
            pots.add(pot1);

//            TextView   textView=new TextView(context);
//            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
//                    LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT
//            );
//            textView.setLayoutParams(params);
//            textView.setText("adfasdf");
//            textView.setTextSize(20);
//            textView.setTextColor(BLUECOLOR);
//            viewList.add(textView);
        }
        totalPots.addAll(pots);
        //记录可见view最右边的坐标
        pointPath.lineTo(getWidth(), chartHeight - (test[test.length - 1]*zoomFacter + baseHeight));
        Pot pot2 = new Pot();
        pot2.x = getWidth();
        pot2.y = chartHeight - (test[test.length - 1]*zoomFacter + baseHeight);
        totalPots.add(pot2);
        //记录屏幕外终点坐标
        pointPath.lineTo(getWidth() + getWidth() / 3, chartHeight);
        Pot pot3 = new Pot();
        pot3.x = getWidth() + getWidth() / 3;
        pot3.y = chartHeight;
        totalPots.add(pot3);
        invalidate();
    }
    class Pot {
        int x;
        int y;
    }
}
